package medicalDocumentation.medicalDoc;

import entity.MedicalCase;
import entity.Medication;
import entity.MedicineEntity;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.Stateful;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

@Stateful
public class CaseMedicineSessionBean implements CaseMedicineSessionBeanRemote {

    @PersistenceContext
    private EntityManager em;
    private MedicalCase medicalCase;
    private List<MedicineEntity> medicineEntityList = new ArrayList<>();
    ArrayList<MedicineEntity> res;
    private MedicineEntity medicineEntity;

    public CaseMedicineSessionBean() {
    }

    @Override
    public void deleteMedicine(Long CIN, Long ID) {
        MedicineEntity medicine = em.find(MedicineEntity.class, ID);
        MedicalCase mCase = em.find(MedicalCase.class, CIN);
        List<MedicineEntity> ml = mCase.getMedicines();
        ml.remove(medicine);
        mCase.setMedicines(ml);
        em.remove(medicine);

        em.flush();
    }

    @Override
    public List<Medication> getMedication() {
        String strQuery = "SELECT m from Medication m";
        Query query = em.createQuery(strQuery);
     System.out.println(query.getResultList().size());
        return query.getResultList();

    }

    @Override
    public ArrayList<MedicineEntity> displayCaseMedicine(Long cin) {
        
        MedicalCase mCase = em.find(MedicalCase.class, cin);
        res = new ArrayList();

        for (int i = 0; i < mCase.getMedicines().size(); i++) {
            if(mCase.getMedicines().get(i).getStatus().equals("Unexecuted")){
                res.add(mCase.getMedicines().get(i)); 
            }
           
        }
        return res;
    }

    @Override
    public Long addMedicine(Long CIN, String code, String name, double dose, int duration, String staus) {
        String status="Unexecuted";
        System.out.println("Session bean ----- add Medicine called");
        System.out.println("Session bean ----- add Medicine called code" + code);
        medicineEntity = new MedicineEntity(code, name, dose,duration,status);

        System.out.println("Session bean ----- add Medicine called" + medicineEntity.getMedicineName() + medicineEntity.getCode() + medicineEntity.getDose());

        System.out.println("Session bean ----- add Medicine called" + CIN);
        medicalCase = em.find(MedicalCase.class, CIN);

        System.out.println("Session bean ----- add Medicine called" + medicalCase.getCIN());
        System.out.println("Session bean ----- add Medicine called" + medicineEntity.getCode());

        List<MedicineEntity> ml = medicalCase.getMedicines();
                ml.add(medicineEntity);
                medicalCase.setMedicines(ml);
        //     medicalCase.setMedicines(medicineEntityList);
//             
        em.persist(medicineEntity);

        em.flush();
        
        return medicineEntity.getId();
    }

    @Override
    public MedicalCase getMedicalCase() {
        return medicalCase;
    }

    @Override
    public void setMedicalCase(MedicalCase medicalCase) {
        this.medicalCase = medicalCase;
    }

    @Override
    public List<MedicineEntity> getMedicineEntityList() {
        return medicineEntityList;
    }

    @Override
    public void setMedicineEntityList(List<MedicineEntity> medicineEntityList) {
        this.medicineEntityList = medicineEntityList;
    }

    @Override
    public MedicineEntity getMedicineEntity() {
        return medicineEntity;
    }

    @Override
    public void setMedicineEntity(MedicineEntity medicineEntity) {
        this.medicineEntity = medicineEntity;
    }

    @Override
    public ArrayList<MedicineEntity> getRes() {
        return res;
    }

    @Override
    public void setRes(ArrayList<MedicineEntity> res) {
        this.res = res;
    }
}
